package com.tamecode.chapter06.sec6;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @Author: Qiongchao
 * @Version: 1.0.0
 * @Date: 2023/10/11
 */
public class MailSender implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void sendMail(String to) {
        System.out.println("MailSender: 模拟发送邮件...");
        MailSendEvent mailSendEvent = new MailSendEvent(this.applicationContext, to);

        // ②向容器中的所有事件监听器发送事件
        applicationContext.publishEvent(mailSendEvent);
    }

}
